<script setup>
import {
  onMounted,
  reactive,
  ref
} from 'vue';
import {
  getMainPageData
} from '@/api/getData.js';
import * as echarts from "echarts";
/* 变量定义开始 */

//用户总数
const userTotal = ref(8888);
//学生总数
const studentTotal = ref(8888);
//课程总数
const courseTotal = ref(8888);
//考试人次
const scoreTotal = ref(8888);
//echarts饼状图
const option = reactive({
  title: {
    text: '年级人数分布',
    subtext: '每个年级人数',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [{
    name: '班级/人数',
    type: 'pie',
    radius: '50%',
    data: [{
      value: 1048,
      name: '一年级'
    },
      {
        value: 735,
        name: '二年级'
      },
      {
        value: 580,
        name: '三年级'
      },
      {
        value: 484,
        name: '四年级'
      },
      {
        value: 300,
        name: '五年级'
      }
    ],
    emphasis: {
      itemStyle: {
        shadowBlur: 20,
        shadowOffsetX: 10,
        shadowColor: 'rgba(0, 0, 0, 0.5)'
      }
    }
  }]
})
/* 变量定义结束 */


/* 函数开始 */
//获取头部4个数据
const getCountData = () => {
  //请求后台获取数据
  getMainPageData().then(res => {
    if (res != -1) {
      //绑定数据
      userTotal.value = res.datas.userTotal;
      studentTotal.value = res.datas.studentTotal;
      courseTotal.value = res.datas.courseTotal;
      scoreTotal.value = res.datas.scoreTotal;
    }
  })
}

//渲染图表
const renderEcharts = () => {
  //渲染
  let myCharts = echarts.init(document.getElementById('echartsDomId'));
  myCharts.setOption(option)
}
/* 函数结束 */

onMounted(() => {
  getCountData(); //顶部4条数据的初始化
  renderEcharts();
})
</script>

<template>
  <div class="main-main">
    <div class="main-header">
      <div class="main-count-item">
        <div class="main-count-icon">
          <UserFilled class="icon" />
        </div>
        <div class="main-count-content">
          <div class="main-count-title">用户总数</div>
          <div class="main-count-number">{{userTotal}}人</div>
        </div>
      </div>
      <div class="main-count-item">
        <div class="main-count-icon">
          <HomeFilled class="icon" />
        </div>
        <div class="main-count-content">
          <div class="main-count-title">学生总数</div>
          <div class="main-count-number">{{studentTotal}}人</div>
        </div>
      </div>
      <div class="main-count-item">
        <div class="main-count-icon">
          <TrendCharts class="icon" />
        </div>
        <div class="main-count-content">
          <div class="main-count-title">课程总数</div>
          <div class="main-count-number">{{courseTotal}}科</div>
        </div>
      </div>
      <div class="main-count-item">
        <div class="main-count-icon">
          <Platform class="icon" />
        </div>
        <div class="main-count-content">
          <div class="main-count-title">考试人次</div>
          <div class="main-count-number">{{scoreTotal}}人/次</div>
        </div>
      </div>
    </div>
    <div class="main-card">
      <el-card class="box-card">
        <template #header>
          <div>
            <span>年级人数分析图表</span>
          </div>
        </template>
        <div id="echartsDomId" class="echarts-class">

        </div>
      </el-card>
      <el-card  class="box-card">
        <template #header>
          <div>
            <span>年级人数分析图表</span>
          </div>
        </template>
      </el-card>
      <el-card  class="box-card">
        <template #header>
          <div>
            <span>年级人数分析图表</span>
          </div>
        </template>
      </el-card>
    </div>
  </div>

</template>

<style scoped>
.main-main {
  background-color: rgb(240, 242, 245);
  height: 93vh;
  padding: 20px;
  padding-top: 50px;
}

.main-header {
  display: flex;
  justify-content: space-between;
}

.main-count-item {
  background-color: white;
  width: 23%;
  height: 110px;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 10px;
  box-shadow: 0px 0px 20px 0px rgb(210, 210, 210);
}

.main-count-icon {
  color: rgb(64, 201, 198);
  width: 90px;
  height: 90px;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.5s;
  border-radius: 10px;
}

.main-count-icon:hover {
  color: white;
  background-color: rgb(64, 201, 198);
}

.icon {
  width: 50px;
  height: 50px;
}

.main-count-content {
  margin-right: 5px;
}

.main-count-title {
  color: rgb(140, 140, 200);
  font-size: 20px;
  font-weight: bolder;
}

.main-count-number {
  margin-top: 5px;
  color: rgb(102, 102, 102);
  font-size: 20px;
  font-weight: bolder;
}

.main-card {
  margin-top: 50px;
  display: flex;
  justify-content: space-between;
}

.box-card {
  width: 600px;
  height: 550px;
}
.echarts-class{
  width: 550px;
  height: 500px;
}
</style>